# -*- coding: utf-8 -*-
# $ Time    :2022/7/2 9:25
# $ Author  :@柒筱暮
# $ GitHub  :https://github.com/Angie-co
# $ File    :start.py

import settings
import components
import items
from threading import Thread


def getdata():
    collect_data = components.CollectData()
    datas_generator = collect_data.parseData
    return datas_generator


def comment_keywords(datas_generator) -> dict:
    ck = components.CountKeyword(datas_generator)
    if settings.WORDS_CSV:
        ck.toCsv()
    return ck.sortRank()


def wordcloud(rank_dict) -> None:
    pw = components.PaintingWordcloud(rank=rank_dict)
    pw.painting()


def save_to_csv(data: dict) -> None:
    s2c = items.Save2Csv(data=data)
    s2c.toCsv()


def save_to_mongo(data: dict) -> None:
    s2m = items.Save2Mongo(data=data)
    s2m.toMongo()


if __name__ == '__main__':
    data_generator1 = getdata()
    data_generator2 = getdata()
    # 保存到csv并获取到排序好的词频
    rank = comment_keywords(data_generator1)

    if settings.IS_PAINTING:
        # 画词云图
        wordcloud(rank_dict=rank)

    for data in data_generator2:
        if settings.ISTO_CSV:
            # 保存数据到csv
            save_to_csv(data=data)
        if settings.ISTO_MONGO:
            # 保存数据到MongoDB
            save_to_mongo(data=data)

